﻿using System;
using ClassLib.Classes.Common;
using DevExpress.Utils;
using DevExpress.XtraSplashScreen;
using ISumen;
using Sumen.Business;
using Sumen.Business.Model.Presenter.Category;
using Sumen.Business.Model;
using Sumen.Business.Model.Presenter.Sys;
using Sumen_Stock.LookAndFeel.Sumen_System;

namespace Sumen_Stock.LookAndFeel.Category
{
    public partial class Frm_Product_Category : DevExpress.XtraEditors.XtraForm
    {
        private FromCode _fromCode = FromCode.FrmCategory;
        private CategoryPresenter _categoryPresenter;


        private Frm_Product_Category_Edit _frmProductCategoryEdit;
        private WaitDialogForm _wait;
        private EnumFormStatus _enumForm;


        public Frm_Product_Category()
        {
            InitializeComponent();
            _categoryPresenter = new CategoryPresenter();
            //if (ClsPublic.Writelog == "1")
            //{
            //    SysLogPresenter log = new SysLogPresenter();
            //    log.Add(_fromCode, EnumFormStatus.View, "", "");
            //}
        }

        public EnumFormStatus EnumForm
        {
            get { return _enumForm; }
            set
            {
                _enumForm = value;
            }
        }


        public override void Refresh()
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            base.Refresh();
            treeList1.DataSource = _categoryPresenter.GetAll();
            treeList1.ExpandAll();
            SplashScreenManager.CloseForm();
        }

        private void Frm_Product_Category_Load(object sender, EventArgs e)
        {
            treeList1.DataSource = _categoryPresenter.GetAll();
            treeList1.ExpandAll();
        }

        private void buttonsArray1_btnEventAdd_click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            if (_frmProductCategoryEdit == null)
            {
                _frmProductCategoryEdit = new Frm_Product_Category_Edit();
                _frmProductCategoryEdit.EnumStatus = EnumFormStatus.Add;
                SplashScreenManager.CloseForm();
                _frmProductCategoryEdit.ShowDialog();
            }
            else
            {
                _frmProductCategoryEdit.EnumStatus = EnumFormStatus.Add;
                SplashScreenManager.CloseForm();
                _frmProductCategoryEdit.ShowDialog();
            }
            Refresh();
        }

        private void buttonsArray1_btnEventDelete_click(object sender, EventArgs e)
        {
            object id = treeList1.FocusedNode[treeList1.KeyFieldName];
            if (id != null && id != "")
            {
                if (ClsMsgBox.XacNhanXoaThongTin())
                {
                    _categoryPresenter.Delete(id);
                    Refresh();
                }
            }
            else
            {
                ClsBaoLoi.ThongTin("Vui lòng chọn sản phẩm");
            }
        }

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonsArray1_btnEventUpdate_click_1(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            object id = treeList1.FocusedNode[treeList1.KeyFieldName];
            if (id != null || id != "")
            {
                if (_frmProductCategoryEdit == null)
                {
                    _frmProductCategoryEdit = new Frm_Product_Category_Edit();
                    _frmProductCategoryEdit.CategoryId = Convert.ToInt32(id);
                    _frmProductCategoryEdit.EnumStatus = EnumFormStatus.Modify;
                    SplashScreenManager.CloseForm();
                    _frmProductCategoryEdit.ShowDialog();
                }
                else
                {
                    _frmProductCategoryEdit.CategoryId = Convert.ToInt32(id);
                    _frmProductCategoryEdit.EnumStatus = EnumFormStatus.Modify;
                    SplashScreenManager.CloseForm();
                    _frmProductCategoryEdit.ShowDialog();
                }
                Refresh();
            }
            else
            {
                ClsMsgBox.ThongTin("Vui lòng chọn danh mục.");
            }
        }



    }
}